//
//  VVideoTitleTextField.m
//  vliour
//
//  Created by huangbolun on 2017/10/16.
//  Copyright © 2017年 huiian. All rights reserved.
//

#import "VVideoTitleTextField.h"

@interface VVideoTitleTextField ()

@end

@implementation VVideoTitleTextField

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.textColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    UIView *bottomLine = [UIView new];
    [self addSubview:bottomLine];
    bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
    [[bottomLine.bottomAnchor constraintEqualToAnchor:self.bottomAnchor] setActive:YES];
    [[bottomLine.heightAnchor constraintEqualToConstant:0.5] setActive:YES];
    [[bottomLine.leftAnchor constraintEqualToAnchor:self.leftAnchor] setActive:YES];
    [[bottomLine.rightAnchor constraintEqualToAnchor:self.rightAnchor] setActive:YES];
    bottomLine.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *titlePlaceHolder = [UILabel new];
    self.titlePlaceHolder = titlePlaceHolder;
    [self addSubview:titlePlaceHolder];
    titlePlaceHolder.translatesAutoresizingMaskIntoConstraints = NO;
    [[titlePlaceHolder.topAnchor constraintEqualToAnchor:self.topAnchor] setActive:YES];
    [[titlePlaceHolder.leftAnchor constraintEqualToAnchor:self.leftAnchor] setActive:YES];
    [[titlePlaceHolder.rightAnchor constraintEqualToAnchor:self.rightAnchor] setActive:YES];
    [[titlePlaceHolder.bottomAnchor constraintEqualToAnchor:bottomLine.topAnchor constant:0] setActive:YES];
    
    titlePlaceHolder.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    titlePlaceHolder.font = self.font;

    [self addTarget:self action:@selector(onInputChanged:) forControlEvents:UIControlEventEditingChanged];
}

- (void)setFont:(UIFont *)font{
    [super setFont:font];
    self.titlePlaceHolder.font = font;
}

- (CGRect)textRectForBounds:(CGRect)bounds{
    return CGRectInset(bounds, 0, 10);
}

- (CGRect)editingRectForBounds:(CGRect)bounds{
    return CGRectInset(bounds, 0, 10);
}

#pragma mark - Action
- (void)onInputChanged:(UITextField *)textFild{
    if (textFild.text.length > 0) {
        self.titlePlaceHolder.hidden = YES;
    }else{
        self.titlePlaceHolder.hidden = NO;
    }
}

@end
